package com.entrogy.service.impl;

import com.entrogy.bean.Appraisal;
import com.entrogy.bean.Attendance;
import com.entrogy.bean.PassRecordEntity;
import com.entrogy.dao.AppraisalMapper;
import com.entrogy.service.AppraisalService;
import com.entrogy.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 考勤的service 实现层
 * @Author 许红松
 * @date 2019/5/9 0009 上午 11:38
 */
@Service("appraisalService")
public class AppraisalServiceImpl  implements AppraisalService {


    @Autowired
    private AppraisalMapper appraisalMapper;




    /**
     *  查询是否存在某张表
     * @param table_name
     * @return
     */
    @Override
    public int selectIstable(String table_name){
        return  1;
    };


    /**
     *  根据school_code 查询dingding E应用配置
     * @param school_code
     * @return
     */
    @Override
    public int selectDingDingConfig(int school_code){
        return appraisalMapper.selectDingDingConfig(school_code);
    };

    /**
     *   查询数据
     * @param map
     * @return
     */
    @Override
    public List<String> getAppraisalList(Map<String,Object> map){
        String table_name=getTable_name("appraisal");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.getAppraisalList(map);
    };

    /**
     *  批量新增
     * @param map
     * @return
     */
    @Override
    public  int  addBachAppraisal(Map<String,Object> map){
        String table_name=getTable_name("appraisal");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.addBachAppraisal(map);
    };


    /**
     *  根据当前日期时间获取表名
     * @return
     */

    public String  getTable_name(String table1){
        String result1= DateUtils.GetNowDate().substring(0,4);//年
        String result2= DateUtils.GetNowDate().substring(5,7);//月
        String result3= DateUtils.GetNowDate().substring(8,10);//日
        String  table_name=table1+"_"+result1+"_"+result2;
        if (result3.equals("01")){
            int code=appraisalMapper.selectIstable(table_name);//查询是否存在该表
            if (code==1){
                return table_name;
            }else{
                throw new UnsupportedOperationException ();//不支持的操作异常
            }
        }else{
            return table_name;
        }

    }

    /**
     *  查询打卡流水信息
     * @param map
     * @return
     */
    @Override
    public List<Appraisal>  selectAppraisalByList(Map<String,Object> map){
        String table_name=getTable_name("appraisal");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.selectAppraisalByList(map);
    };
    /**
     *  查询总数
     *
     * @return
     */
    @Override
    public  int total(Map<String,Object> map){
        String table_name=getTable_name("appraisal");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        int code=0;
        if (appraisalMapper.total(map)!=null && appraisalMapper.total(map)!=0){
            code=appraisalMapper.total(map);
        }
        return code;
    };

    /**
     *  创建下月的考勤表
     * @param
     * @return
     */
    @Override
    public int createtable(){
       String  table_name="appraisal_"+DateUtils.GetNowDate().substring(0,4)+"_"+DateUtils.GetNowDate().substring(5,7);
        return appraisalMapper.createtable(table_name);
    };

    /**
     *  钉钉查询打卡流水信息
     * @param map
     * @return
     */
    @Override
    public List<Appraisal>  getAppraisalByList(Map<String,Object> map){
        String table_name=getTable_name("appraisal");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.getAppraisalByList(map);
    };

    /**
     *   根据考勤Id  查询详情
     * @param map
     * @return
     */
    @Override
    public  Appraisal  selectAppraisalById(Map<String,Object> map){
        String table_name=getTable_name("appraisal");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.selectAppraisalById(map);
    };

    /**
     *  批量新增
     * @param map
     * @return
     */
    @Override
    public  int  addBachAttendance(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.addBachAttendance(map);
    };

    /**
     *  钉钉查询打卡结果信息
     * @param map
     * @return
     */
    @Override
    public List<String>  getAttendanceList(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.getAttendanceList(map);
    };

    /**
     *  钉钉查询打卡结果信息列表
     * @param map
     * @return
     */
    @Override
    public List<Attendance>  getAttendanceByList(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.getAttendanceByList(map);

    };

    /**
     *  查询总数
     *
     * @return
     */
    @Override
    public  Integer getTotal(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        int code=0;
        if (appraisalMapper.getTotal(map)!=null && appraisalMapper.getTotal(map)!=0){
            code=appraisalMapper.getTotal(map);
        }
        return code;
    };

    /**
     *   查询考勤月度
     * @param map
     * @return
     */
   /* @Override
    public  List<Map<String,Object>>   getListByAll(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }

        return appraisalMapper.getListByAll(map);
    };*/

    /**
     *  获取当天的考勤数据
     * @return
     */

    public  List<PassRecordEntity>   getPassRecordList(Map<String,Object> map){
        String result1= DateUtils.GetNowDate().substring(0,4);//年
        String result2= DateUtils.GetNowDate().substring(5,7);//月
        String result3= DateUtils.GetNowDate().substring(8,10);//日
        String table_name="pass_record"+result1+result2+result3;
        map.put("table_name",table_name);
        return appraisalMapper.getPassRecordList1(map);
    };

    /**
     *  根据人员查询他本人当天的考勤时间
     * @param map
     * @return
     */
    @Override
    public  Map<String,Object>   getshiftBypeople(Map<String,Object> map){
        if (DateUtils.getWek().equals("星期一")){
            map.put("shift","shift1Id");
        }else if(DateUtils.getWek().equals("星期二")){
            map.put("shift","shift2Id");
        }else if(DateUtils.getWek().equals("星期三")){
            map.put("shift","shift3Id");
        }else if(DateUtils.getWek().equals("星期四")){
            map.put("shift","shift4Id");
        }else if(DateUtils.getWek().equals("星期五")){
            map.put("shift","shift5Id");
        }else if(DateUtils.getWek().equals("星期六")){
            map.put("shift","shift6Id");
        }else if(DateUtils.getWek().equals("星期日")){
            map.put("shift","shift7Id");
        }
        return appraisalMapper.getshiftBypeople(map);
    };

    /**
     *  修改状态
     * @param map
     * @return
     */
    @Override
    public  int  updateStype1(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.updateStype1(map);
    };

    /**
     *  查询统计所有
     * @param map
     * @return
     */
    @Override
    public List<Map<String,Object>>   getStatisticsList(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        List<Map<String,Object>> list= appraisalMapper.getStatisticsList(map);
        for (Map<String,Object> smap:list) {
            map.put("personId",smap.get("user_Id"));
            if(appraisalMapper.getCount1List(map)!=null){
                smap.put("days",appraisalMapper.getCount1List(map).size());
            }else {
                smap.put("days", "0");
            }
        }
        return appraisalMapper.getStatisticsList(map);
    };

    /**
     *  查询出勤天数所有
     * @param map
     * @return
     */
    @Override
    public List<String> getCount1List(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.getCount1List(map);
    };

    /**
     *  钉钉查询打卡结果信息
     * @param map
     * @return
     */
    @Override
    public List<Attendance>  getAttendanceList1(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.getAttendanceList1(map);
    };

    /**
     *  钉钉查询打卡结果信息
     * @param map
     * @return
     */
    @Override
    public List<String>  getAttendanceList2(Map<String,Object> map){
        String table_name=getTable_name("attendance");
        if (table_name!=null && !"".equals(table_name)){
            map.put("table_name",table_name);
        }
        return appraisalMapper.getAttendanceList2(map);
    };


    /**
     *  创建下月的考勤表
     * @param
     * @return
     */
    @Override
    public int createtable1(){
        String  table_name=getTable_name("attendance");
        return appraisalMapper.createtable1(table_name);
    };
}
